package com.article.service.impl;

import com.article.lucene.ArticleIndex;
import com.article.model.ArticleInfo;
import com.article.model.ArticleType;
import com.article.service.ArticleTypeService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.File;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/4/28.
 */
@Component
public class InitArticleIndex implements ServletContextListener, ApplicationContextAware {

    private static ApplicationContext applicationContext;
    //问题索引
    private ArticleIndex questionIndex = new ArticleIndex();

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext application = servletContextEvent.getServletContext();
        //String path = "C://luceneart";
        //File file = new File(path);
        //if (!file.exists()) {
        //    if (file.mkdir()) {
        //QuestionService questionService = (QuestionService) applicationContext.getBean("questionService");
        //List<Question> questions = questionService.listByPage(new HashMap<String, Object>());
        //for (Question ques : questions) {
        //    try {
        //        questionIndex.addIndex(ques);
        //    } catch (Exception e) {
        //        e.printStackTrace();
        //    }
        //}
        //    }
        //}

    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
